//
//  HBEmotionManager.swift
//  sinaWeibo
//
//  Created by Beans on 16/7/2.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit

class HBEmotionManager: NSObject {

    // 总共四组表情
    static var recentEmotion: [HBEmotion] = [HBEmotion]()
    static var defaultEmotion: [HBEmotion] = HBEmotionManager.readEmotion("com.sina.default") //[HBEmotion]()
    static var emojiEmotion: [HBEmotion] = HBEmotionManager.readEmotion("com.apple.emoji") //[HBEmotion]()
    static var lxhEmotion: [HBEmotion] = HBEmotionManager.readEmotion("com.sina.lxh") //[HBEmotion]()

    class func addRecentEmotion(emotion: HBEmotion) {
        // 判断重复
        // 第一种方式: 遍历
        // 第二种方式: 数组的contains方法, 能够判断重复
        if !recentEmotion.contains(emotion) {
            // 把最近的放在第一个, 超过20个时删除最后一个
            recentEmotion.insert(emotion, atIndex: 0)
            if recentEmotion.count >= 20 {
                recentEmotion.removeLast()
            }
        }

        // 存取
    }

    // 把四组表情放在一个数组里, 方便读取 (声明成类方法, 方便调用)
    class func getAllEmotion() -> [[[HBEmotion]]] {
        // [二维数组, 二维数组, ..], 返回的是三维数组
        return [pageEmotions(recentEmotion), pageEmotions(defaultEmotion), pageEmotions(emojiEmotion), pageEmotions(lxhEmotion)]
    }

    /// 读取表情包数据 (以default为例, 再修改参数或抽取)
    /// filePath: 表情包的文件夹名称
    class func readEmotion(filePath: String) -> [HBEmotion] {
        // 1. 读取bundle
        let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        // 2. 拼接包名称
        let packagePath = (bundlePath as NSString).stringByAppendingPathComponent(filePath)
        // 3. 拼接info.plist
        let infoPath = (packagePath as NSString).stringByAppendingPathComponent("info.plist")
        // 4. 读取字典plist
        let dict = NSDictionary(contentsOfFile: infoPath)!
//        printLog(dict)
        // 5. 获取表情数组字典
        let emotionsDict = dict["emoticons"] as! [[String : AnyObject]]

        // 6. 遍历数组, 字典转模型
        var resultArr = [HBEmotion]()
        for emotionDict in emotionsDict {
            let model = HBEmotion(dict: emotionDict)
            model.path = packagePath
            resultArr.append(model)
        }

        return resultArr
    }

    // 对数组进行分割, 每页20个
    class func pageEmotions(emotions: [HBEmotion]) -> [[HBEmotion]] {
        // 1. 页数
        let page = (emotions.count - 1) / 20 + 1
        // 2. 初始化数组, 接收切割的数据
        var resultArrArr = [[HBEmotion]]()
        // 3. 循环进行切割
        for i in 0..<page {
            // 起始位置
            let loc = 20 * i
            // 切割长度 最后一组要考虑边界问题
            var len = 20
            if loc + len > emotions.count {
                len = emotions.count - loc
            }
            let subArr = (emotions as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [HBEmotion]
            // 添加到数组
            resultArrArr.append(subArr)
        }

        return resultArrArr
    }

    /// 通过字符串查找表情对象
    class func searchEmotion(text: String) -> HBEmotion? {
        // 遍历
        for emotion in defaultEmotion {
            if emotion.chs == text {
                return emotion
            }
        }

        for emotion in lxhEmotion {
            if emotion.chs == text {
                return emotion
            }
        }

        return nil
    }
}
